package com.xinli.xinlifm.view.fragment.forum;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.ForumBean;
import com.xinli.xinlifm.http.bean.FroumDetailCommentBean;
import com.xinli.xinlifm.view.activity.LoadActivity;
import com.xinli.xinlifm.view.activity.SecondActivity;
import com.xinli.xinlifm.view.adapter.SuperRecyclerAdapter;

import java.util.List;

/**
 * Created by XiaoLiu on 2016/10/12.
 */

public class FroumFragmentAdapter extends SuperRecyclerAdapter<Object,FroumFragmentAdapter.ViewHolder> implements View.OnClickListener {
    public int type;
    AlertDialog alertDialog = null;
    private ViewHolder playHolder;

    public void setType(int type) {
        this.type = type;
    }

    public FroumFragmentAdapter(Context context, List<Object> list) {
        super(context, list, new int[]{R.layout.forum_item, R.layout.forum_detail_header,R.layout.forum_detail_comment});
    }


    public void setLastShow(int tag){
        switch (tag) {
            case 1:
                playHolder.one.setVisibility(View.VISIBLE);
                playHolder.two.setVisibility(View.INVISIBLE);
                break;
            case 2:
                playHolder.one.setVisibility(View.INVISIBLE);
                playHolder.two.setVisibility(View.VISIBLE);
                break;
        }
    }


    @Override
    public int getToChild(int position) {
        Log.e("e", "类名==FroumFragmentAdapter" + "方法名==getToChild=gtg====:"+type);
        if(type == 0){
            return layoutId[0];
        }else if(type == 1){
            Object obj = list.get(position);
            if(obj instanceof ForumBean.DataBean){
                return layoutId[1];
            }else if(obj instanceof FroumDetailCommentBean.DataBean){
                Log.e("f", "类名==FroumFragmentAdapter" + "方法名==getToChild=====:" + "frgrg");
                return layoutId[2];
            }
        }
        return 0;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        playHolder = holder;
        if(position == list.size()){
            return;
        }
        if(type == 0 && getToChild(position)== layoutId[0]){
            ForumBean.DataBean bean = ((ForumBean.DataBean) list.get(position));
            holder.bean = bean;
            Picasso.with(context).load(bean.getUser().getAvatar()).into(holder.forum_image);
            holder.nickName.setText(bean.getUser().getNickname());
            holder.content.setText(bean.getContent());
            holder.updated.setText(bean.getUpdated());
            holder.commentnum.setText(bean.getCommentnum()+"");
            holder.title.setText(bean.getTitle());
            holder.imageContainer.removeAllViews();
            if(bean.getImages().size() > 0){
                for (int i = 0; i < bean.getImages().size(); i++) {
                    if( i>=3){
                        break;  //只给显示三张图片
                    }
                    if(i >= 1){
                        //添加图片之间的分割线
                        View view = new View(context);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(12,200);
                        view.setLayoutParams(layoutParams);
                        holder.imageContainer.addView(view);
                    }
                    ImageView imageView = new ImageView(context);
                    Picasso.with(context).load(bean.getImages().get(i)).into(imageView);
                    //这里要对imageview设置高宽
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(250, 250);
                    imageView.setLayoutParams(layoutParams);
                    holder.imageContainer.addView(imageView);
                }
            }
            //做点击事件的处理
            holder.title.setOnClickListener(holder);
            holder.content.setOnClickListener(holder);
            holder.imageContainer.setOnClickListener(holder);
            holder.updated.setOnClickListener(holder);
        } else if(type == 1 && getToChild(position)!=layoutId[0]){
            if(getToChild(position) == layoutId[1]){  //头布局
                ForumBean.DataBean bean = ((ForumBean.DataBean) list.get(position));
                Picasso.with(context).load(bean.getUser().getAvatar()).into(holder.forum_image);
                holder.nickName.setText(bean.getUser().getNickname());
                holder.content.setText(bean.getContent());
                holder.updated.setText(bean.getUpdated());
                holder.title.setText(bean.getTitle());
                holder.imageContainer.removeAllViews();

                holder.other.setOnClickListener(this);

                if(bean.getImages().size() > 0){
                    for (int i = 0; i < bean.getImages().size(); i++) {
                        if( i>=3){
                            return;  //只给显示三张图片
                        }
                        if(i >= 1){
                            //添加图片之间的分割线
                            View view = new View(context);
                            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300,10);
                            view.setLayoutParams(layoutParams);
                            holder.imageContainer.addView(view);
                        }
                        ImageView imageView = new ImageView(context);
                        Picasso.with(context).load(bean.getImages().get(i)).into(imageView);
                        //这里要对imageview设置高宽
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300);
                        imageView.setLayoutParams(layoutParams);
                        holder.imageContainer.addView(imageView);
                    }
                }
            }else if (getToChild(position) == layoutId[2]){
                FroumDetailCommentBean.DataBean bean = ((FroumDetailCommentBean.DataBean) list.get(position));
                Log.e("自定义标签", "类名==FroumFragmentAdapter" + "方法名==onBindViewHolder=====:" + bean);
                if(null != bean.getUser().getAvatar()){
                    Picasso.with(context).load(bean.getUser().getAvatar()).into(holder.avater);
                }
                holder.nickName.setText(bean.getUser().getNickname());
                holder.content.setText(bean.getContent());
                holder.created.setText(bean.getCreated());
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.other:
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                View view1 = LayoutInflater.from(context).inflate(R.layout.forum_dialog,null);
                view1.findViewById(R.id.inform).setOnClickListener(this);
                view1.findViewById(R.id.cancel).setOnClickListener(this);
                builder.setView(view1);
                alertDialog = builder.create();
                alertDialog.show();
                break;
            case R.id.inform:
                Intent intent = new Intent(context, LoadActivity.class);
                context.startActivity(intent);
                break;
            case R.id.cancel:
                alertDialog.dismiss();
                break;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        //  进度显示
        private LinearLayout one;
        private TextView two;

        private ImageView forum_image;
        private TextView nickName;
        private TextView updated;
        private TextView title;
        private TextView content;
        private TextView commentnum;
        private ForumBean.DataBean bean;
        private LinearLayout imageContainer;
        private ImageView other; // 举报 ，取消

        //评论列表的
        private ImageView avater;
        private TextView created;
        public ViewHolder(View itemView) {
            super(itemView);


            //downLoad 布局中
            one = (LinearLayout) itemView.findViewById(R.id.one);
            two = (TextView) itemView.findViewById(R.id.two);

            forum_image = (ImageView) itemView.findViewById(R.id.forum_image);
            nickName = (TextView) itemView.findViewById(R.id.nickName);
            updated = (TextView) itemView.findViewById(R.id.updated);
            title = (TextView) itemView.findViewById(R.id.title);
            content = (TextView) itemView.findViewById(R.id.content);
            commentnum = (TextView) itemView.findViewById(R.id.commentnum);
            imageContainer = (LinearLayout) itemView.findViewById(R.id.iamge_container);
            other = (ImageView) itemView.findViewById(R.id.other);
            avater = (ImageView) itemView.findViewById(R.id.avatar);
            created = (TextView) itemView.findViewById(R.id.created);
        }
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                //跳转到详情页面的
                case R.id.updated:
                case R.id.content:
                case R.id.forum_image:
                case R.id.iamge_container:
                    Intent intent = new Intent(view.getContext(), SecondActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bean", bean);
                    intent.putExtra("type","detail");
                    intent.putExtras(bundle);
                    view.getContext().startActivity(intent);
                    break;
            }
        }
    }
}
